<?php
/**
* エラーチェッククラス
**/
require_once( '../system/Class_M_DAO.php' );
class Class_M_COST_DM_PRICE  extends Class_M_DAO {
		//protected properties begin
	/**
	 *
	 * テーブル名
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	protected $_table  = "M_COST_DM_NUMFROM";
	protected $_table_sub    = "M_COST_DM_PRICE";
	
	/**
	 *
	 * 主キー名
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	protected $_pkey   = "id";
	
	/**
	 *
	 * ソート順
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	 protected $_order = " num_from";
	
	 protected $_cols  = array(
	 	"year4"       => array(AOKI_VARTYPE_STR),
		"num_from"    => array(AOKI_VARTYPE_STR),
		"num_to"      => array(AOKI_VARTYPE_STR),

	 );
	
	 protected $_checks  = array(
	 	"year4"       => array( "EXIST" ,"NUM" ),
		"num_from"    => array( "NUM"  ),
		"num_to"      => array( "NUM" ),
	 );
/*	 
	 //maxlength 
	 protected $_maxlens  = array(
		"dm_no "      =>  10 
	 );
	*/
	 public function search($where ,&$page = NULL ){	
	 	
		$sql = "SELECT a.*,(select  user_name FROM M_LOGIN where id  = a.u_user) as m_user_name from {$this->_table}  a " ;
		return  parent::search($where ,$page ,$sql);
	}
	 public function searchSub($where){	
	 	
		$sql = "SELECT b.* from  {$this->_table}  a " .
				" inner join  {$this->_table_sub}  b on a.id = b.nfid  " ;
		$sql = $sql." where ".implode(" AND ", $where);
	 	$this->_db->query( $sql );
		$data = array();
		while( $row = $this->_db->fetch() ){
			$data[$row["nfid"]][$row["tyid"]] = $row["price"];
		}
		return $data;
	}
	 public function delInsertSub($nfid, $values ){	
	 	
	 	//削除
	 	$sql = "delete from {$this->_table_sub} where nfid = {$nfid}";
	 	$delOK = FALSE;
	 	try {
			if ($this->_db->query( $sql )) {
				
				$delOK =  TRUE;
			} else {
				return FALSE;
			}
		} catch (Exception $e) {
			echo "例外キャッチ：", $e->getMessage(), "\n";
			return FALSE;
		}
		
		//追加
		$insarr = false;
		foreach ($values  as $k => $v) {
			$insarr[] = "({$nfid}, {$k},'{$v}')" ;
		}
		
		if ($insarr) {
			$sql =  "insert into {$this->_table_sub}(nfid,tyid,price) values ".implode(",",$insarr);
		}
		try {
			if ($this->_db->query( $sql )) {
				
				return TRUE;
			} else {
				return FALSE;
			}
		} catch (Exception $e) {
			echo "例外キャッチ：", $e->getMessage(), "\n";
			return FALSE;
		}
	}
	 public function getSub($nfid ){
		$sql ="SELECT * from {$this->_table_sub}  where nfid = {$nfid} ";
	 	$this->_db->query( $sql );
		$data = array();
		while( $row = $this->_db->fetch() ){
			$data[$row["tyid"]] = $row["price"];
		}
		return $data;
	 }	
	
	 public function do_num_to( $year4, $maxineger = 2147483647 ){	
		$sql = "update {$this->_table} a , " .
			"(select b.id,(select  min(num_from) as num_from  from   {$this->_table} where  num_from > b.num_from and year4= {$year4}  ) as num_to " .
			" from {$this->_table}  b ) c " .
			"set a.num_to = IF( c.num_to is null, {$maxineger}, c.num_to -1 )  where a.id = c.id and a.year4= {$year4}";
			try {
			if ($this->_db->query( $sql )) {
				return TRUE;
			} else {
				return FALSE;
			}
		} catch (Exception $e) {
			echo "例外キャッチ：", $e->getMessage(), "\n";
			return FALSE;
		}
	}
	


	
}

?>